

#include"stdafx.h"
#include"math.h"
#include"conio.h"
#include"cv.h"
#include"highgui.h"
#include"stdio.h"
#include"VideoCapture.h"

#define TRESH 50
#define STALL 1
int main()

{

int i,j,k;

int height,width,step,channels;

 
int stepr, channelsr;

int temp=0;

uchar *data,*datar;


i=j=k=0;

//cvNamedWindow("Device 0");
VideoCapture capt(0,320,240);

IplImage * frame = capt.CreateCaptureImage();

//IplImage *frame=cvLoadImage("b.jpg",1);

//cvNamedWindow("original",CV_WINDOW_AUTOSIZE);


IplImage *result=cvCreateImage(cvGetSize(frame), 8, 1 );

if(frame==NULL ) {
puts("unable to load the frame");exit(0);}
printf("frame loaded");
cvNamedWindow("Result",CV_WINDOW_AUTOSIZE);
char c=0;
while(c!='q') {
capt.waitFrame(frame);
height = frame->height;
width = frame->width;
step =frame->widthStep;
channels = frame->nChannels;
data = (uchar *)frame->imageData;
/*Here I use the Suffix r to diffenrentiate the result data and the frame data
29
Example :stepr denotes widthStep of the result IplImage and step is for frame IplImage*/
stepr=result->widthStep;
channelsr=result->nChannels;
datar = (uchar *)result->imageData;
for(i=0;i < (height);i++)
for(j=0;j <(width);j++)
/*As I told you previously you need to select pixels which are
more red than any other color Hence i select a difference of 29(which again depends on the scene).
(you need to select randomly and test*/

if((((data[i*step+j*channels+2]) > (TRESH+data[i*step+j*channels]))&& ((data[i*step+j*channels+2]) > (TRESH+data[i*step+j*channels+1]))))
datar[i*stepr+j*channelsr]=255;
else
datar[i*stepr+j*channelsr]=0;
c=cvWaitKey(STALL);
cvShowImage("Result",result);

}

capt.stop();



/*If you want to use cvErode you may...*/

/*Destroying all the Windows*/

/*cvShowImage("original",frame);

cvShowImage("Result",result);

cvSaveImage("result.jpg",result);*/

//cvWaitKey(0);

cvDestroyWindow("original");
cvDestroyWindow("Result");
return 0;

}
